<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/2/2 0002
 * Time: 9:54
 */

namespace app\index\controller;


use app\index\model\AuthGroup;
use app\index\model\Member as MemberModel;
use app\index\model\AuthGroupAccess;

class Member extends Base
{
    private $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new MemberModel();
    }

    public function index()
    {
        return $this->fetch();
    }

    public function lst()
    {
        $start = $this->request->get('start', 0);
        $length  = $this->request->get('length', 15);
        $search  = $this->request->get('keyword');
        $where = [];
        if ($search) {
            $where[] = ['username', 'like', "%{$search}%"];
        }
        $data = $this->model->getList($start, $length, $where);
        $total = $this->model->getListCount($where);
        $result = [
            'draw' => $this->request->get('draw'),
            'recordsTotal' => $total,
            'recordsFiltered' => $total,
            'data' => $data
        ];
        return json($result);
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $result = $this->validate($data, 'app\index\validate\Member.add');
            if ($result !== true) {
                $this->error($result);
            }
            $group_id = $data['group_id'];
            unset($data['group_id'], $data['rpassword']);
            $data['salt'] = createNonceStr(5);
            $data['password'] = createPassword($data['password'], $data['salt']);
            $memberModel = new MemberModel;
            $res = $memberModel->save($data);
            if ($res) {
                $memberModel->groups()->attach($group_id);
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }
        $auth_group = new AuthGroup();
        $group = $auth_group->getList(0, 100);
        $this->assign('group', $group);
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $id = $this->request->post('id', 0);
            $result = $this->validate($data, 'app\index\validate\Member.edit');
            if ($result !== true) {
                $this->error($result);
            }
            //判断是否需要修改密码
            if ($data['password']) {
                if (!$data['rpassword']) {
                    $this->error('重复密码必须');
                }
                if ($data['password'] != $data['rpassword']) {
                    $this->error('两次密码不一致');
                }
                $data['salt'] = createNonceStr(5);
                $data['password'] = createPassword($data['password'], $data['salt']);
            }else{
                unset($data['password']);
            }
            $group_id = $data['group_id'];
            unset($data['group_id'], $data['rpassword']);
            $res = $this->model->save($data, ['id' => $id]);
            //$res = $this->model->edit($id, $data);
            if ($res) {
                $auth_group_access = new AuthGroupAccess();
                $auth_group_access->where('uid', $id)->update(['group_id' => $group_id]);
                $this->success('编辑成功');
            }
            $this->error('编辑失败');
        }

        $id = $this->request->get('id');
        $member = MemberModel::with('groups')->where("id", $id)->findOrFail()->toArray();
        //var_dump($member);exit;
        //$member = $this->model->getOneById($id);
        $auth_group = new AuthGroup();
        $group = $auth_group->getList(0, 100);
        $this->assign('group', $group);
        $this->assign('member', $member);
        return $this->fetch();
    }

    public function del()
    {
        $id = $this->request->get('id');
        if ($this->request->isAjax()) {
            $res = $this->model->del($id);
            if ($res) {
                $auth_group_access = new AuthGroupAccess();
                $auth_group_access->where("uid", $id)->delete();
                $this->success('删除成功');
            }
            $this->error('删除失败');
        }
    }
}